layui.config({
	base: 'assets/module/'
}).extend({
	formSelects: 'formSelects/formSelects-v4',
	treetable: 'treetable-lay/treetable',
	dropdown: 'dropdown/dropdown',
	notice: 'notice/notice',
	step: 'step-lay/step',
	dtree: 'dtree/dtree',
	citypicker: 'city-picker/city-picker',
	area: 'area/select',
	tableSelect: 'tableSelect/tableSelect'
}).use(['layer', 'element', 'config', 'index', 'admin', 'laytpl'], function() {
	var $ = layui.jquery;
	var layer = layui.layer;
	var element = layui.element;
	var config = layui.config;
	var index = layui.index;
	var admin = layui.admin;
	var laytpl = layui.laytpl;

	admin.removeLoading();
	layer.closeAll('loading');

    if (!config.getToken() || !config.getUser()) {
        return location.replace('login.html');
    }

	setInterval(function () {
		var  t = sessionStorage.getItem("logintime");;
		if(!t) return location.replace('login.html');
		if(new Date().getTime()> t*1000*3600*2){
			refreshToken()
		}
	},1000*3600*2)
	function refreshToken(){
		$.post(serverUrlAdmin + 'User/refreshToken',
			{
				"token":config.getToken(),
				"refresh_token":config.getUser().refresh_token
			}
			, function (res) {
				layer.closeAll('loading');
				if (res.code == 200) {
					config.putToken(res.obj.token);
					sessionStorage.setItem("logintime", res.obj.token_out_time);
				} else {
					layer.msg('登录已失效，请重新登录', {icon: 2, time:300}, function () {
						config.putToken(null);
						config.putUser(null);
						sessionStorage.clear();
						return location.replace('login.html');
					})
				}
			}, 'json');
		return false;
	}

    var menuJson = config.getUser().authorities;
	menuJson.unshift(
		{
			"name": "首页",
			"icon": "layui-icon layui-icon-website",
			"url": "#/welcome"
		}
	)

	// 其他界面路由跳转
	var OtherRouter = [{
			"name": "编辑",
			"url": "#/knowledge/edit"
		},{
			"name": "详情",
			"url": "#/knowledge/detail"
		}
	];
	
	laytpl(sideNav.innerHTML).render(menuJson, function(html) {
		$('.layui-layout-admin .layui-side .layui-nav').html(html);
		element.render('nav');
	});
	index.regRouter(menuJson); // 注册路由
	index.regRouter(OtherRouter); // 其他界面路由
	index.loadHome({ // 加载主页
		url: '#/welcome',
		name: '<i class="layui-icon layui-icon-home"></i>'
	});

});



